package com.lanou3g.k12.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Section;
import com.lanou3g.k12.service.SectionService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController  //关于图书的增删改查
public class SectionController {


        @Autowired
        private SectionService sectionService;

        @GetMapping("/chapter/sections/{cpId}")
        //通过按钮传来的ID查询到所有节,返回给前端
        public K12Result findAllSection(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                       String keyword, @RequestParam(value = "size",defaultValue = "30") Integer size
                                        ,@PathVariable Long cpId){
            try{
                Page objects;
                if (keyword!= null &&!keyword.trim().equals("")){
                    objects = PageHelper.startPage(page, size);
                    sectionService.search(page,keyword,size,cpId);
                }else {
                    objects = PageHelper.startPage(page, size);
                    sectionService.findAllSection(page,size,cpId);
                }
                return K12ResultUtils.ok(objects);
            }catch (Exception e){
                e.printStackTrace();
                return K12ResultUtils.error(500);
            }

        }
        //批量删除
        @DeleteMapping("/chapter/sections/{ids}")
        public K12Result deleteSomeSection(@PathVariable Long[] ids){
            try{
                sectionService.deleteSomeSection(ids);
                return K12ResultUtils.ok();
            }catch (Exception e){
                e.printStackTrace();
                return K12ResultUtils.error(500);
            }
        }

        //根据id删除单个
        @DeleteMapping("/chapter/section/{id}")
        public K12Result deleteSomeSection(@PathVariable Long id){
            try{
                sectionService.deleteOneSection(id);
                return K12ResultUtils.ok();
            }catch (Exception e){
                e.printStackTrace();
                return K12ResultUtils.error(500);
            }
        }

        //添加
        @PostMapping("/chapter/section")
        public K12Result insertSection(Section section){
            try {
                sectionService.insertSection(section);
                System.out.println(section);
                return K12ResultUtils.ok();
            }catch (Exception e){
                e.printStackTrace();
                return K12ResultUtils.error(500);
            }
        }
        //修改
        @PutMapping("/chapter/section/{id}")
        public K12Result updateSection(@PathVariable Long id, Section section) {
            try {
                sectionService.updateSection(id,section);
                return K12ResultUtils.ok();
            }catch (Exception e){
                e.printStackTrace();
                return K12ResultUtils.error(500);
            }

        }

}
